home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 2 / Gold Medal Software Volume 2 (Gold Medal) (1994).iso / graphics / plotter.arj / MKDATA.C < prev    next >
Text File  |  1993-08-06  |  6KB  |  219 lines

  1.  
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <stdarg.h>
  5. #include <ctype.h>
  6. #include <stdlib.h>
  7. #include <stdlib.h>
  8.  
  9.  
  10. /* --------- program to make binary data files for PLOTTER ------------- */
  11. /* ------- to use, modify function f() as desired and recompile -------- */
  12.  
  13.  
  14. float f(float x, float z)
  15. {
  16.   float a,b,c,d,e;
  17.  
  18.   a = (float)(x*x+z*z);
  19.   b = (float)sqrt(a);
  20.   c = (float) fabs(cos(7.0*b));
  21.   d = sqrt(c);
  22.   if (fabs(1.0 + 30.0*a) <= 10e-4) {
  23.     printf("Denominator near 0\n");
  24.     printf("x = %.2f, z=%.2f, b = %.2f, c = %.2f\n",x,z,b,c);
  25.     getch();
  26.   }
  27.   e = (float)(((2.0*d))/(1.0 + 30.0*a));
  28.   return e;
  29. }
  30.  
  31. #define MAXDIV  101
  32. int getint(char *x, ...);     /* extract ints from string s */
  33. int getfloat(char *x, ...);   /* extract floats from string s */
  34.  
  35. main()
  36. {
  37.   FILE *fp;
  38.  
  39.   int i,j,num;
  40.   float deltaX, deltaZ;
  41.   float X[MAXDIV],Z[MAXDIV];
  42.   float Y[MAXDIV][MAXDIV];
  43.  
  44.   float Xmin,Xmax,Zmin,Zmax;
  45.   int numX,numZ;
  46.   char filename[32];
  47.   int theta0=0,alpha0=0;
  48.   char title[71],s[80];
  49.  
  50.   clrscr();
  51.   while (1) {
  52.     printf("\tEnter name for data file: "); gets(filename);
  53.     if (!filename[0]) exit(0);
  54.     printf("\tEnter function title (formula, etc. 70 chars max:\n\t"); gets(s);
  55.     j=strlen(s);
  56.     for (i=0; i<j; i++) title[i] = s[i];
  57.     for (i=j; i<70; i++) title[i] = ' ';
  58.     title[70] = 0;              /* cut off after 70 characters */
  59. a:
  60.     printf ("\tEnter Xmin, Xmax, Zmin, Zmax: "); gets(s);
  61.     if (!s[0]) exit(1);
  62.     if (getfloat(s, &Xmin, &Xmax, &Zmin, &Zmax) != 4) {
  63.       printf("\tPlease reenter\n");
  64.       goto a;
  65.     }
  66. b:
  67.     printf("\tEnter number of x_divs, y_divs: "); gets(s);
  68.     if (!s[0]) exit(0);
  69.     if (getint(s, &numX, &numZ) != 2) {
  70.       printf("\tPlease reenter\n");
  71.       goto b;
  72.     }
  73.     printf("\tEnter default angles theta and alpha (-45 to 45): "); gets(s);
  74.     if (!s[0]) exit(0);
  75.     if (getint(s, &theta0, &alpha0) != 2) {
  76.       printf("\tPlease reenter\n");
  77.       goto b;
  78.     }
  79.     printf("\tEvaluating ");
  80.     deltaX = (float)(Xmax - Xmin)/(numX-1);
  81.     deltaZ = (float)(Zmax - Zmin)/(numZ-1);
  82.     for (i=0; i<numX; i++) {
  83.       X[i] = Xmin + i*deltaX;
  84.     }
  85.     for (i=0; i<numZ; i++) {
  86.       Z[i] = Zmin + i*deltaZ;
  87.     }
  88.     for (j=0; j<numZ; j++) {
  89.       for (i=0; i<numX; i++) {
  90.         Y[j][i] = f(X[i],Z[j]);
  91.       }
  92.       printf("*");
  93.     }
  94.     printf("\n\tWriting ");
  95.     fp = fopen(filename,"w+b");
  96.     fwrite(title, sizeof(char), 70, fp);
  97.     fwrite(&numX, sizeof(int), 1, fp);
  98.     fwrite(&numZ, sizeof(int), 1, fp);
  99.     fwrite(X, sizeof(float), numX, fp);
  100.     fwrite(Z, sizeof(float), numZ, fp);
  101.     for (j=0; j<numZ; j++) {
  102.       fwrite(&Y[j], sizeof(float), numX, fp);
  103.       printf(".");
  104.     }
  105.     fwrite(&theta0, sizeof(int), 1, fp);
  106.     fwrite(&alpha0, sizeof(int), 1, fp);
  107.     fclose(fp);
  108.  
  109.     printf("\n\n\tPress R to repeat, else to quit... "); i = getche();
  110.     printf("\n");
  111.     if (i != 'r' && i != 'R') break;
  112.   }
  113. }
  114.  
  115.  
  116. /*
  117.    extract integers from string s, put them into specified variables
  118.    arguments should be pointers to ints, e.g. getint(s,&x1,&x2,&x3).
  119.    Assumes numbers are written in standard ASCII form: no isolated or repeated
  120.    minus signs, no decimals, no spaces inside numbers, no exponents, etc.
  121. */
  122.  
  123. int
  124. getint(char *t, ...)
  125. {
  126.   int cnt=0;
  127.   char *p,s[80];
  128.   int *arg;
  129.   va_list ap;
  130.  
  131.   s[0] = ' '; s[1] = '\0';
  132.   strcat(s,t);             /* be sure string starts with a space */
  133.   va_start(ap,s);
  134.   p = s;
  135.   while ((arg = va_arg(ap, int *)) != NULL)
  136.     {
  137.      /* move past invalid numerical characters */
  138. a:
  139.      while (*p && !isdigit(*p) && *p != '-') p++;
  140.      if (!*p)
  141.        break;
  142.      /* minus signs must be preceded by a space, followed by a digit */
  143.      if (*p == '-') {
  144.        if (*(p-1) != ' ' || !isdigit(*(p+1))) {
  145.          p++;
  146.          goto a;
  147.        }
  148.      }
  149.      *arg = atoi(p);
  150.      if (*p == '-') p++;                 /* skip leading minus sign */
  151.      while(isdigit(*p)) p++;             /* move through remaining valids */
  152.      cnt++;                              /* finished with current number */
  153.     }
  154.   return cnt;
  155. }
  156.  
  157. /* 
  158.    extract float values from string s, put them into specified variables
  159.    arguments should be pointers to floats, e.g. getfloat(s,&x1,&x2,&x3)
  160.    assumes numbers are written in standard ASCII form: no isolated or repeated
  161.    minus signs or decimals, no spaces inside numbers, no exponents, etc.
  162. */
  163.  
  164. int
  165. getfloat(char *t, ...)
  166. {
  167.   int cnt=0;
  168.   char *p,s[80];
  169.   float *arg;
  170.   va_list ap;
  171.  
  172.   s[0] = ' '; s[1] = '\0';
  173.   strcat(s,t);             /* be sure string starts with a space */
  174.   va_start(ap,s);
  175.   p = s;
  176.   while ((arg = va_arg(ap, float *)) != NULL)
  177.     {
  178.      /* move past invalid numerical characters */
  179. a:
  180.      while (*p && !isdigit(*p) && *p != '-'&& *p != '.')
  181.        p++;
  182.      if (!*p)
  183.        break;
  184.      /* decimal points must be preceded by a digit or a space and followed
  185.         by a digit or space */
  186.      if (*p == '.') {
  187.        if ((*(p-1) != ' ' && !isdigit(*(p-1))) ||
  188.           ((*(p+1) != ' ' && !isdigit(*(p+1))))) {
  189.          p++;
  190.          goto a;
  191.        }
  192.      /* and if preceded by a space, must be folloewd by a digit */
  193.        if (*(p-1) == ' ' && !isdigit(*(p+1))) {
  194.          p++;
  195.          goto a;
  196.        }
  197.      }
  198.      /* minus signs must be preceded by a space and followed by a digit */
  199.      if (*p == '-') {
  200.        if (*(p-1) != ' ' || !isdigit(*(p+1))) {
  201.          p++;
  202.          goto a;
  203.        }
  204.      }
  205.      *arg = atof(p);
  206.      if (*p == '-' || *p == '.') p++;     /* skip leading minus or decimal */
  207.      while(isdigit(*p) || *p == '.') {
  208.       if(*p == '.' && !isdigit(*(p+1))) {
  209.         p++;
  210.         continue;
  211.       }
  212.       p++; /* move through remaining valids */
  213.     }
  214.    cnt++;                               /* finished with current number */
  215.   }
  216.   return cnt;
  217. }
  218.  
  219.